import yaml
import threading
import sys
import os


class Conf:
    yaml = None

    __lock = threading.Lock()

    def __new__(cls, *args, **kwargs):
        if not hasattr(Conf, "_Conf__instance"):
            with Conf.__lock:
                if not hasattr(Conf, "_Conf__instance"):
                    Conf.__instance = object.__new__(cls)
        return Conf.__instance

    def __init__(self):
        if not hasattr(self, "_Conf__init"):
            with Conf.__lock:
                if not hasattr(self, "_Conf__init"):
                    self.__init = True
                    # path = sys.path[0]
                    # path = sys.path[1]
                    path = os.path.dirname(os.path.dirname(__file__))
                    # print(path)
                    active = self.__load(f'{path}/resources/application.yaml')
                    Conf.yaml = self.__load(f'{path}/resources/application-{active["mdc"]["active"]}.yaml')

    @classmethod
    def __load(cls, path) -> dict:
        with open(path, 'r', encoding='utf-8') as yf:
            return yaml.load(yf.read(), Loader=yaml.Loader)


Conf()
